import javax.swing.*; 
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
public class WindowCalender extends JFrame implements ItemListener,TreeSelectionListener {
   JTable table;
   JTree tree=null;    // 树组件
   DefaultMutableTreeNode root;
   Object name[]={"星期日","星期一","星期二","星期三", "星期四","星期五","星期六"};
   JComboBox  yearList;
   int year,month;
   CalendarBean calendar;
   String rili[][];
   String item[]={"2010","2011","2012","2013","2014","2015","2016" };
   JScrollPane scrollTable,scrollTree;
   JSplitPane split;
   WindowCalender() {
       init();
       setSize(580,200);
       setVisible(true);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
   } 
   void init() {
      calendar=new  CalendarBean();
      yearList=new JComboBox();
      for(int k=0;k<item.length;k++) {
         yearList.addItem(item[k]);
      }
      yearList.addItemListener(this);
      root=new DefaultMutableTreeNode(item[0]); //创建根节点. 
      year=Integer.parseInt(item[0]);
      month=1;
      DefaultMutableTreeNode [] 月=new DefaultMutableTreeNode[13];//子节点数组
      for(int i=1;i<=12;i++) {
        月[i]=new DefaultMutableTreeNode(""+i);
        root.add(月[i]); 
      }
      tree=new JTree(root);
      add(new JScrollPane(tree),BorderLayout.WEST);
      tree.addTreeSelectionListener(this); //监视树节点事件
      calendar.setYear(year); 
      calendar.setMonth(month);
      rili=calendar.getCalendar();
      table=new JTable(rili,name); 
      scrollTree=new JScrollPane(tree);
      scrollTable=new JScrollPane(table);
      split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scrollTree,scrollTable);
      split.setDividerLocation(0.5);
      add(yearList,BorderLayout.NORTH);
      add(split,BorderLayout.CENTER);
   }
   public void itemStateChanged(ItemEvent e) { //处理下拉列表上的ItemEvent事件
      String 年=yearList.getSelectedItem().toString().trim();
      year=Integer.parseInt(年);
      calendar.setYear(year);
      root=new DefaultMutableTreeNode(年);         //重新创建根节点
      DefaultMutableTreeNode 月[]=new DefaultMutableTreeNode[13];//子节点数组
      for(int i=1;i<=12;i++) {
          月[i]=new DefaultMutableTreeNode(""+i);
          root.add(月[i]); 
      }
      split.remove(scrollTree);
      tree=new JTree(root); 
      tree.addTreeSelectionListener(this);  
      scrollTree=new JScrollPane(tree);
      split.add(scrollTree,JSplitPane.LEFT);
   }   
   public void valueChanged(TreeSelectionEvent e) { //处理TreeSelectionEvent事件
      DefaultMutableTreeNode  monthNode=
      (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
      if(monthNode.isLeaf()) {
          month=Integer.parseInt(monthNode.toString().trim());
          calendar.setMonth(month);          rili=calendar.getCalendar();
          split.remove(scrollTable);
          table=new JTable(rili,name); 
          scrollTable=new JScrollPane(table);
          split.add(scrollTable,JSplitPane.RIGHT); 
      }
   }
}
